﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Search
{
    public class RecursiveBinarySearch : ISearch
    {
        private int RbinSearch<T>(IList<T> mynums, T value, int lower, int upper) where T : IComparable<T>
        {
            if (lower > upper)
            {
                return -1;
            }
            else
            {
                int mid;
                mid = (int)(upper + lower) / 2;
                int compare = value.CompareTo(mynums[mid]);
                if (compare < 0)
                {
                    return RbinSearch(mynums, value, lower, mid - 1);
                }
                else if (mynums[mid].CompareTo(value) == 0)
                {
                    return mid;
                }
                else
                {
                    return RbinSearch(mynums, value, mid + 1, upper);
                }
            }
        }

        public int Perform<T>(IList<T> list, T value) where T : IComparable<T>
        {
            return RbinSearch(list, value, 0, list.Count - 1);
        }
    }
}
